const app = new Vue({
    el: '#registerBox',
    data: function () {
        return {
            info: {
                type: '药品生产商',
                username: '',
                firstPwd: '',
                secondPwd: '',
                phone: ''
            },
            account: {
                username: '',
                password: '',
                type: '',
                phoneNumber: ''
            }
        }
    },
    methods: {
        cancelReg: function () {
            this.info = {
                type: '药品生产商',
                username: '',
                firstPwd: '',
                secondPwd: '',
                phone: ''
            }
        },
        register: function () {
            if (this.info.type === '') {
                this.$message.warning('请选择用户类型！');
                return;
            }
            if (this.info.username === '') {
                this.$message.warning('请填写用户名！');
                return;
            }
            if (this.info.firstPwd === '') {
                this.$message.warning('请填写密码！');
                return;
            }
            if (this.info.secondPwd === '') {
                this.$message.warning('请填写确认密码！');
                return;
            }
            if (this.info.phone === '') {
                this.$message.warning('请填写电话号码！');
                return;
            }

            if (this.info.firstPwd !== this.info.secondPwd) {
                this.$message.error('密码和确认密码应相同，请重新尝试！');
                this.info.firstPwd = '';
                this.info.secondPwd = '';
                return;
            }

            if (this.info.type === '药品生产商') {
                this.account.type = 1;
            } else if (this.info.type === '销售商') {
                this.account.type = 2;
            } else if (this.info.type === '监管部门') {
                this.account.type = 3;
            }

            this.account.username = this.info.username;
            this.account.password = this.info.firstPwd;
            this.account.phoneNumber = this.info.phone;

            let that = this;
            axios.post('/register', that.account).then(
                function (response) {
                    console.log(response);
                    let code = response.data.code;
                    if (code === 200) {
                        that.$message.success('注册成功，请返回登录！');
                    } else if (code === 1005) {
                        that.$message.error('用户名已存在！');
                        this.cancelReg();
                    }
                }, function (err) {
                    console.log(err)
                })
        }
    },
});